// NeLNS - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
// Copyright (C) 2010  Winch Gate Property Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

#ifndef LS_LOGIN_MGR_H
#define LS_LOGIN_MGR_H

#include <nel/misc/singleton.h>
#include <server_share/mysql_helper.h>

#include <queue>
#include <map>

//#include <mysql_connection.h>  
//#include <mysql_driver.h>  
//#include <cppconn/statement.h>  

#include <nel/net/callback_server.h>
#include "public_def.h"

namespace LS
{

class CLoginMgr : public NLMISC::CSingleton<CLoginMgr>
{
public:

	bool init();

	void CallbackServer( NLNET::CCallbackServer* pCallbackServer ){ _pCallbackServer = pCallbackServer; }
	NLNET::CCallbackServer* CallbackServer() { return _pCallbackServer; }

	bool isFull();
	bool push( SLoginRequest* pLoginRequest );

	SLoginRequest* findUser( std::string& user );
	SLoginRequest* findUser( DEF::UID uid );

	void removeUser( std::string& user );
	void removeSock( NLNET::TSockId sock );

	bool update();

	bool addLoginMsgTimeOut( NLNET::TSockId sock );
	bool removeLoginMsgTimeOut( NLNET::TSockId sock );

private:

	SLoginRequest* pop();

	typedef std::map<std::string,SLoginRequest*>      TVerifyMap;
	typedef std::map<NLNET::TSockId,std::string>      TSockMap;
	typedef std::map<NLNET::TSockId,NLMISC::TTicks>   TLoginMsgTimeOutMap;

	uint32                 _MaxPlayers;
	uint32                 _CurrPlayers;
	NLMISC::TTicks         _CurrTicks;
	NLMISC::TTicks         _PreLoginMsgTicks;
	uint32                 _LoginConnectMax;
	uint32                 _LoginQueueMax;
	uint32                 _LoginMsgTimeOutTick;
	uint32                 _LoginMsgTimeOutFPS;
	

	NLNET::CCallbackServer*     _pCallbackServer;
	MSH::CConnection            _AccountDB;
	std::queue<std::string>     _LoginQueue;

	TVerifyMap             _VerifyMap;
	TSockMap               _SockMap;
	TLoginMsgTimeOutMap	   _LoginMsgTimeOutMap;


	//sql::mysql::MySQL_Driver *driver;  
	//sql::Connection *con;  
	//sql::Statement *state;  
	//sql::ResultSet *result;  

};

#define  LoginMgr  CLoginMgr::instance()

}
#endif // LS_LOGIN_MGR_H

/* End of login_mgr.h */
